package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.model.Menu;
import com.sanjiahantang.ythshopping.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */

public class ClassifyHolder extends MyBaseViewHolder {

    private List<View> gridList;
    private List<ImageView> imageviews;
    private Context context;
    private List<Menu> datas;
    private ViewPager vpMenu;
    private LinearLayout llImage;
    AdPageAdapter adapter;
    MainActivity activity;
    private int type;
    private int count = 0;
    public ClassifyHolder(View itemView, Context context, List<Menu> datas, MainActivity activity,int type) {
        super(itemView);
        this.context = context;
        this.datas = datas;
        this.activity = activity;
        vpMenu= (ViewPager) itemView.findViewById(R.id.vp_home_menu);
        llImage= (LinearLayout) itemView.findViewById(R.id.ll_home_page);
        gridList=new ArrayList<>();
        this.type = type;
        count = datas.size();

    }

    @Override
    void bindData() {
        imageviews=new ArrayList<>();
        if( count < 6){
            vpMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,Utils.dip2px(context,170 / 2)));
        }else {
            vpMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,Utils.dip2px(context,170 )));
        }
        while (datas.size()!=0)
        {
            List<Menu> itemgrid=new ArrayList<>();

            for (int i=0;i<10;i++)
            {
                if (datas.size()!=0)
                {
                    itemgrid.add(datas.get(0));
                    datas.remove(0);
                }else {
                    break;
                }
            }
            GridView gridView=new MyGridView(context);
            gridView.setNumColumns(5);

            gridView.setAdapter(new GvSubjectAdapter(context, itemgrid, activity,type));
            gridList.add(gridView);
           ImageView imageview=new ImageView(context);
            LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
                    10, 10);
            layoutParam.setMargins(10, 0, 10, 0);
            imageview.setLayoutParams(layoutParam);
            if (imageviews.size()==0)
            {
                imageview.setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_red));

            }else{
                imageview.setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_normal));
            }
            imageviews.add(imageview);
            llImage.addView(imageview);


        }
        adapter=new AdPageAdapter(gridList);
        vpMenu.setAdapter(adapter);
        vpMenu.setOnPageChangeListener(new AdPageChangeListener());



    }

    private final class AdPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {

            for (int i = 0; i < imageviews.size(); i++) {
                if (arg0 != i) {
                    imageviews.get(i).setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_normal));
                } else {
                    imageviews.get(i).setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_red));
                }
            }
        }
    }



    private final class AdPageAdapter extends PagerAdapter {
        private List<View> views = null;

        public AdPageAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(View container, final int position) {
            ((ViewPager) container).addView(views.get(position), 0);
            return views.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
